﻿using System;
using System.Windows.Threading;
using AzureDiagMon.Common.Mvvm.Interfaces;
using GalaSoft.MvvmLight;
using Ninject;

namespace AzureDiagMon.Common.Mvvm
{
    public abstract class ViewModel : ViewModelBase, IViewModel
    {
        protected Dispatcher Dispatcher = DispatcherHelper.Current;

        [Inject]
        protected IInteractionService InteractionService { get; set; }

        protected ViewModel()
        {
            if (IsInDesignMode)
            {
                Bootstrapper.Startup();
            }
            else
            {
                IoC.Inject(this);
            }
        }

        protected bool ShowDialog(IChildViewModel viewModel)
        {
            return InteractionService.ShowDialog(viewModel);
        }

        protected bool ShowDialog(object view, Action<IChildViewModel> initializeViewModel = null)
        {
            IChildViewModel childViewModel;
            return InteractionService.ShowDialog(view, out childViewModel, initializeViewModel);
        }

        protected bool ShowDialog(object view, out IChildViewModel childViewModel, Action<IChildViewModel> initializeViewModel = null)
        {
            return InteractionService.ShowDialog(view, out childViewModel, initializeViewModel);
        }

        protected void Invoke(Action action)
        {
            if (action == null)
                throw new ArgumentNullException("action");

            if (Dispatcher.CheckAccess())
            {
                action();
            }
            else
            {
                Dispatcher.BeginInvoke(action);
            }
        }
    }
}
